/*!
 * myPagination Jquery Pagination Plug-in Library v5.0
 * 
 * http://linapex.blog.163.com/
 *
 * Date: 2012/9/26 17:23
 */
(function ($) {
    $.fn.myPagination = function (param) {
        return init(param, $(this))
    };

    function init(param, obj) {
        var defaults = {
            currPage: 1,
            pageCount: 10,
            pageNumber: 10,
            cssStyle: 'badoo',
            ajax: {
                on: false,
                type: "POST",
                pageCountId: 'pageCount',
                url: "jsonTest.php",
                dataType: 'json',
                param: false,
                onClick: function () {
                    return false
                },
                ajaxStart: function () {
                    return false
                },
                callback: function (data) {
                    return false
                }
            },
            panel: {
                first: '首页',
                last: '尾页',
                next: '下一页',
                prev: '上一页',
                first_on: true,
                last_on: true,
                next_on: true,
                prev_on: true,
                links: '#',
                tipInfo_on: false,
                tipInfo: '<span>&nbsp;&nbsp;跳{currText}/{sumPage}页</span>',
                tipInfo_css: {width: '22px'}
            }
        };

        function getParam() {
            var param = "page=" + opts.currPage;
            if (opts.ajax.param) {
                param += "&" + opts.ajax.param
            }
            ;
            return param
        };

        function checkInputPage(val) {
            var msg;
            if (val < 1) {
                msg = "输入值不能小于1"
            }
            ;var patrn = /^[0-9]{1,8}$/;
            if (!patrn.exec(val)) {
                msg = "请输入正确的数字"
            }
            ;
            if (val > opts.pageCount) {
                msg = "输入值不能大于总页数"
            }
            ;
            if (msg) {
                alert(msg);
                return false
            }
            ;
            return true
        };

        function getPanelTipInfo() {
            var str = "";
            if (opts.panel.tipInfo_on) {
                var input = "<input type='text' value='" + opts.currPage + "' >";
                var info = opts.panel.tipInfo;
                info = info.replace("{currText}", input);
                info = info.replace("{sumPage}", opts.pageCount);
                info = $(info);
                input = info.children(":text:first");
                var css = opts.panel.tipInfo_css;
                for (var temp in css) {
                    var val = eval("css." + temp);
                    input.css(temp, val)
                }
                ;str = info.html()
            }
            ;
            return str
        };

        function onRequest() {
            debug(opts.id);
            debug("ajax请求参数列表:");
            debug(getParam());
            if (opts.ajax.on) {
                opts.ajax.ajaxStart();
                $.ajax({
                    url: opts.ajax.url,
                    type: opts.ajax.type,
                    data: getParam(),
                    contentType: "application/x-www-form-urlencoded;utf-8",
                    async: true,
                    cache: false,
                    timeout: 60000,
                    error: function () {
                        alert("访问服务器超时，请重试，谢谢！")
                    },
                    success: function (data) {
                        responseHandle(data);
                        createPageBar()
                    }
                })
            } else {
                createPageBar()
            }
        };

        function responseHandle(data) {
            var pageCountId = opts.ajax.pageCountId;
            var resultPageCount = 1;
            switch ((opts.ajax.dataType)) {
                case "json":
                    try {
                        data = eval("(" + data + ")");
                    } catch (err) {
                    } finally {
                        resultPageCount = eval("data." + pageCountId)
                    }
                    ;
                    break;
                case "xml":
                    resultPageCount = $(data).find(pageCountId).text();
                    break;
                default:
                    if (document.all) {
                        resultPageCount = $(data).find("input:hidden[id='" + pageCountId + "']").val();
                    } else {
                        resultPageCount = $(data).filter("input:hidden[id='" + pageCountId + "']").val();
                    }
                    break
            }
            ;debug(opts.id);
            debug("返回总页数:" + resultPageCount);
            opts.pageCount = resultPageCount;
            opts.ajax.callback(data)
        };

        function createPageBar() {
            var links = opts.panel.links;
            opts.currPage = opts.currPage > opts.pageCount ? opts.pageCount : opts.currPage;
            var currPage = opts.currPage;
            var pageCount = parseInt(opts.pageCount);
            var tempPage = parseInt(opts.pageNumber / 2);
            var pageNumber = opts.pageNumber;
            var str = "";
            if (opts.panel.first_on) {
                str = "<a href='" + links + "' title='1'>" + opts.panel.first + "</a>"
            }
            ;
            if (opts.panel.prev_on) {
                if (currPage == 1) {
                    str += "<span class=\"disabled\" title=\"" + opts.panel.prev + "\">" + opts.panel.prev + " </span>"
                } else {
                    str += "<a href='" + links + "' title='" + (currPage - 1) + "'>" + opts.panel.prev + " </a>"
                }
            }
            ;var firstPage = lastPage = 1;
            firstPage = currPage - tempPage > 0 ? firstPage = currPage - tempPage : 1;
            if (firstPage + pageNumber > pageCount) {
                lastPage = pageCount + 1;
                firstPage = lastPage - pageNumber
            } else {
                lastPage = firstPage + pageNumber
            }
            ;
            if (firstPage <= 0) {
                firstPage = 1
            }
            ;
            for (firstPage; firstPage < lastPage; firstPage++) {
                if (firstPage == currPage) {
                    str += "<span class=\"current\" title=\"" + firstPage + "\">" + firstPage + "</span>"
                } else {
                    str += "<a href='" + links + "' title='" + firstPage + "'>" + firstPage + "</a>"
                }
            }
            ;
            if (opts.panel.next_on) {
                if (currPage == pageCount) {
                    str += "<span class=\"disabled\" title=\"" + opts.panel.next + "\">" + opts.panel.next + " </span>"
                } else {
                    str += "<a href='" + links + "' title='" + (currPage + 1) + "'>" + opts.panel.next + " </a>"
                }
            }
            ;
            if (opts.panel.last_on) {
                str += "<a href='" + links + "' title='" + pageCount + "'>" + opts.panel.last + "</a>"
            }
            ;str += getPanelTipInfo();
            debug(opts.id);
            debug("最终生成菜单：");
            debug(str);
            obj.html(str);
            obj.children(":text").keypress(function (event) {
                var keycode = event.which;
                if (keycode == 13) {
                    var page = $(this).val();
                    if (checkInputPage(page)) {
                        obj.children("a").unbind("click");
                        obj.children("a").each(function () {
                            $(this).click(function () {
                                return false
                            })
                        });
                        opts.currPage = page;
                        onRequest();
                    }
                }
            });
            obj.children("a").each(function (index, element) {
                $(this).click(function () {
                    var page = parseInt(this.title);
                    page = page > 0 ? page : 1;
                    obj.children("a").unbind("click");
                    obj.children("a").each(function () {
                        $(this).click(function () {
                            return false
                        })
                    });
                    opts.currPage = page;
                    opts.ajax.onClick(page);
                    onRequest();
                    $(this).focus();
                    return false
                });
            })
        };

        function debug(str) {
            $.fn.debug(str)
        };var opts = $.extend(true, defaults, param);
        opts.id = obj.attr("id");
        obj.addClass(opts.cssStyle);
        onRequest();
        var method = {};
        method.id = opts.id;
        method.getPage = function () {
            return opts.currPage
        };
        method.onReload = function () {
            debug("reload()");
            onRequest()
        };
        method.onLoad = function (param) {
            if (param && param instanceof Object) {
                debug(param);
                opts.currPage = 1;
                opts.ajax.param = param.param;
                onRequest()
            }
        };
        method.jumpPage = function (page) {
            debug("jumpPage()");
            page = page < 1 ? 1 : page;
            page = page > opts.pageCount ? opts.pageCount : page;
            opts.currPage = page;
            onRequest()
        };
        return method
    };$.fn.debug = function (str) {
    }
})(jQuery);